package artist

import (
	"fmt"
	"server/cfg"
	"server/game/bag"
	"server/game/player"
	"server/util"
	"strconv"
	"strings"

	log2 "github.com/name5566/leaf/log"
)

type ArtistLogic struct {
	dao *ArtistDao
}

func NewArtistLogic() *ArtistLogic {
	return &ArtistLogic{dao: NewArtistDao()}
}
func (a *ArtistLogic) NewArtistInfo(userID string, cardId int32) ([]Artists, error) {
	config := cfg.ConfigData.TbBeauty.Get(cardId)
	data := Artists{
		ArtistsId: cardId,
		UserId:    userID,
		Quality:   config.Quality,
		Charm:     float64(config.Influence),
		BeautyLv:  int64(config.BaseIdentity),
	}
	err := a.dao.Insert(userID, data)
	if err != nil {
		log2.Error("创建艺人失败 错误信息是%v", err)
		return nil, err
	}
	return a.ArtistList(userID)
}

func (a *ArtistLogic) ArtistList(userID string) ([]Artists, error) {
	return a.dao.GetArtistsByUid(userID)
}
func (a *ArtistLogic) RandNum(userId string, cardId int32, skillId int32) ([]Artists, error) {
	artistInfo, err := a.dao.GetArtistByUidArtistsId(userId, cardId)
	if err != nil {
		return nil, fmt.Errorf("不要下请求s")
	}
	for key, skillInfo := range artistInfo.BeautySkillList {
		if skillInfo.SkillId == skillId {
			config := cfg.ConfigData.TbBeautySkill.Get(skillId)
			artistInfo.BeautySkillList[key].Offect = util.GetRandomInt(int(config.MinValue), int(config.MaxValue))
		}
	}
	err = a.dao.UpBeautySkill(userId, cardId, artistInfo.BeautySkillList)
	if err != nil {
		return nil, err
	}
	return a.dao.GetArtistsByUid(userId)

}
func (a *ArtistLogic) UnLockArtistBeautySkill(userId string, cardId int32, skillId int32) ([]Artists, error) {
	artistInfo, err := a.dao.GetArtistByUidArtistsId(userId, cardId)
	if err != nil {
		return nil, fmt.Errorf("不要下请求s")
	}
	for _, skillInfo := range artistInfo.BeautySkillList {
		if skillInfo.SkillId == skillId {
			return nil, err
		}
	}
	config := cfg.ConfigData.TbBeautySkill.Get(skillId)
	beautySkillInfo := BeautySkillInfo{
		SkillId: skillId,
		Level:   0,
		Offect:  util.GetRandomInt(int(config.MinValue), int(config.MaxValue)),
	}
	artistInfo.BeautySkillList = append(artistInfo.BeautySkillList, beautySkillInfo)
	err = a.dao.UpBeautySkill(userId, cardId, artistInfo.BeautySkillList)
	if err != nil {
		return nil, err
	}
	return a.ArtistList(userId)
}
func (a *ArtistLogic) UpArtistBeautyLv(userId string, cardId int32, step int64) ([]Artists, error) {
	artist, err := a.dao.GetArtistByUidArtistsId(userId, cardId)
	if err != nil {
		return nil, err
	}
	var config *cfg.BeautyLv
	for _, configTmp := range cfg.ConfigData.TbBeautyLv.GetDataList() {
		if configTmp.Level == int32(artist.BeautyLv+step) {
			config = configTmp
		}
	}
	if config == nil {
		return nil, fmt.Errorf("不能提升等级")
	}
	num, err := a.dao.GetUnArtistsNumByUid(userId)
	if err != nil {
		num = 0
	}
	if float64(config.NeedInfluence) > artist.Charm || config.NeedBeautyNum > int32(num) {
		//return nil, fmt.Errorf("没有够调教，不能解锁")
	}
	artist.BeautyLv = artist.BeautyLv + step
	err = a.dao.UpArtistsBeautyLv(userId, cardId, artist.BeautyLv)
	if err != nil {
		return nil, err
	}
	return a.ArtistList(userId)
}

func (a *ArtistLogic) GetBeautyLvByUidCardId(userId string, cardId int32) (*cfg.BeautyLv, error) {
	artist, err := a.dao.GetArtistByUidArtistsId(userId, cardId)
	if err != nil {
		return nil, err
	}
	config := cfg.ConfigData.TbBeautyLv.Get(int32(artist.BeautyLv))
	return config, nil
}

func (a *ArtistLogic) CalculateCharm(userId string) {
	//artist, err := a.dao.GetArtistsByUid(userId)
	//for {
	//
	//}
}
func (a *ArtistLogic) GiveBeauty(userId string, cardId int32, itemId, itemNum int) (map[string]interface{}, error) {
	var ret = make(map[string]interface{}, 0)
	artist, err := a.dao.GetArtistByUidArtistsId(userId, cardId)
	if err != nil {
		return nil, err
	}
	var qinMiDu int
	switch itemId {
	case 110028:
		qinMiDu = 1
		break
	case 110029:
		qinMiDu = 2
		break
	case 110030:
		qinMiDu = 10
		break
	}
	bagList, err := bag.EnUseItem(userId, itemId, itemNum)
	if err != nil {
		return nil, err
	}
	qinMiDu = qinMiDu * itemNum
	artist.Charm += float64(qinMiDu)
	a.dao.UpBeautyCharm(userId, cardId, artist.Charm)
	ret["bag_list"] = bagList
	ret["artist_info"] = artist
	return ret, nil
}
func (a *ArtistLogic) PublicizeBeauty(userId string, num int) (map[string]interface{}, error) {
	var ret = make(map[string]interface{}, 0)
	userInfo, err := player.EnGetUserInfo(userId)
	if err != nil {
		return nil, err
	}
	if userInfo.XuanChuanTiLi < num {
		return nil, fmt.Errorf("宣传体力没有")
	}
	var artistList = make([]Artists, 0)
	var addCharmCount = 0
	config66 := cfg.ConfigData.TbConfig.Get(66)
	for i := 0; i < num; i++ {
		if userInfo.IsFirst {
			artistId, _ := strconv.Atoi(config66.Value)
			artistInfo, _ := a.dao.GetArtistByUidArtistsId(userId, int32(artistId))
			artistInfo, addCharm, err := a.publicizeBeauty(artistInfo)
			if err != nil {
				return nil, err
			}
			addCharmCount += addCharm
			artistList = append(artistList, artistInfo)
		} else {
			artistInfo, _ := a.dao.GetRandomArtistByUid(userId)
			artistInfo, addCharm, err := a.publicizeBeauty(artistInfo)
			if err != nil {
				return nil, err
			}
			log2.Debug("addCharmCount is %d, addCharm is %d", addCharmCount, addCharm)
			addCharmCount += addCharm
			artistList = append(artistList, artistInfo)
		}
	}
	bagList, err := bag.EnAddItem(userId, 101005, addCharmCount)
	if err != nil {
		return nil, err
	}
	zuanShu := player.EnUserZuanSu(userId)
	config70 := cfg.ConfigData.TbConfig.Get(70)
	zuanShuNum, _ := strconv.Atoi(config70.Value)
	zuanShu = float64(zuanShuNum) * zuanShu
	if _, err = bag.EnAddItem(userId, 101, int(zuanShu)); err != nil {
		return nil, err
	}
	userRet, err := player.EnCostUserXuanChuanTiLi(userId, num)
	if err != nil {
		return nil, err
	}
	ret["user_info"] = userRet["player_info"]
	ret["artist_list"] = artistList
	ret["bag_list"] = bagList
	ret["add_item_one"] = map[string]interface{}{
		"item_id":  101005,
		"item_num": addCharmCount,
	}
	ret["add_item_two"] = map[string]interface{}{
		"item_id":  101,
		"item_num": zuanShu,
	}
	ret["add_charm"] = addCharmCount
	return ret, nil
}

func (a *ArtistLogic) publicizeBeauty(artistInfo Artists) (Artists, int, error) {
	config66 := cfg.ConfigData.TbConfig.Get(66)
	jiaChengNum, _ := strconv.Atoi(config66.Value)
	jiaChengNum = jiaChengNum / 1000
	config72 := cfg.ConfigData.TbConfig.Get(72)
	baoJiHeJiangLiBeiShu := strings.Split(config72.Value, "|")
	baoJi, _ := strconv.Atoi(baoJiHeJiangLiBeiShu[0])
	jiangLiBeiShu, _ := strconv.Atoi(baoJiHeJiangLiBeiShu[1])
	randNum := util.GetRandomInt(1, 99)
	var addCharm int
	addCharm = int(artistInfo.Charm) * jiaChengNum
	if randNum < int32(baoJi) {
		addCharm = jiangLiBeiShu*addCharm + addCharm
	}
	artistInfo.XuanChuanJinYan += addCharm
	a.dao.UpBeautyXuanChuanJinYan(artistInfo.UserId, artistInfo.ArtistsId, artistInfo.XuanChuanJinYan)
	return artistInfo, addCharm, nil
}
func (a *ArtistLogic) UpdateYinXiaoId(userId string, cardId int32, yinXiaoId int64) {
	a.dao.UpBeautyYinXiaoId(userId, cardId, yinXiaoId)
}
